package com.xiaohudie.Optional;

import com.xiaohudie.Lambda.Booke;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.function.Supplier;

/**
 * https://www.cnblogs.com/Kevin-ZhangCG/p/14852547.html
 * @Author xiao hu die
 * @Date 2022/2/27 13:19
 * @Version 1.0
 */
public class test02 {
 public static void main(String[] args) throws Throwable {
  List<Optional<Booke>> booke = getBooke();
  for (Optional<Booke> optionalBooke : booke) {
  // optionalBooke.ifPresent(x-> System.out.println(x.getId()));
  // Booke booke1 = optionalBooke.orElseGet(() -> new Booke("100", 111, 9L));
   Booke booke1 = optionalBooke.orElseThrow(new Supplier<Throwable>() {
    @Override
    public Throwable get() {
     return new RuntimeException("值为空");
    }
   });
   System.out.println(booke1.getId());
  }
 }
 public static List<Optional<Booke>> getBooke() {
  List<Optional<Booke>> bookeList = new ArrayList<>();
  for (int i = 0; i < 10; i++) {
   Booke booke = new Booke(String.valueOf(i), i, 9l);
   Optional<Booke> booke1 = Optional.ofNullable(booke);
   bookeList.add(booke1);
  }
  Booke booke  =null;
  Optional<Booke> booke2 = Optional.ofNullable(booke);
  Booke booke1 = booke2.orElse(new Booke("111", 10000, 6L));
  Optional<Booke> booke11 = Optional.ofNullable(booke1);
  bookeList.add(booke11);
  return bookeList;
 }
}
